# -*- coding: utf-8 -*-
'''
Created on 2011-7-27

@author: Johnny
'''

import sys

from action import Action

class Step(object):
    '''
    Set up and run step actions.
    '''


    def __init__(self, name, key, times, description, expected_result):
        '''
        Constructor.
        Keyword arguments:
        name -- step name, e.g. Feed Input
        key -- try/repeat
        times -- repeat times or try times
        '''
        self.name = name
        self.key = key
        self.times = times
        self.description = description
        self.expected_result = expected_result
        self.actions = []
        self.result = "No Run"
        self.step_pic = ""
        
    def set_up(self, sele):
        # Capture screen shot for every step
        print 'step -- set_up '
        self.add_action('screencapture', '')
        
        for act in self.actions:
            act.setup(sele)
        
    def add_action(self, name, value):
        '''
        Add actions
        '''
        self.actions.append(Action(name, value))
    
    def get_result(self):
        pass
        
    def run(self):
        '''
        '''
        print 'step -- run: step name =  ' + self.name
        i = 0
        self.result = True
        while i < self.times:
            for act in self.actions:
                try:
                    'execute action'
                    result = act.do()
                    
                    if result and act.name == "screencapture":
                        self.step_pic = result
                except Exception, e:
                    print str(e)
                    self.result = "Failed"
                    self.step_pic = self.actions[len(self.actions) - 1].do()
                    
                    raise Exception, str(sys.exc_info()[0]) + ' ' \
                                   + str(sys.exc_info()[1]) + ' ' \
                                   + str(sys.exc_info()[2])
            if self.result:
                self.result = "Passed"
                
            i = i + 1
            if self.key == "try" and self.result:
                break
        